package com.yanqu.road.dao.impl.activity.suitang.data;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangCity;
import com.yanqu.road.entity.activity.suitang.enums.eSuiTangWarType;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

public class CrossSuiTangCityDaoImpl extends TempDataObjectDao<CrossSuiTangCity> {
    public static String getCrossSuiTangCitySql = "select * from `t_c_suitang_city_data` where `activityId`=? and `groupId`=?";
    public Map<Integer, Map<eSuiTangWarType, Map<Integer, Map<Integer, CrossSuiTangCity>>>> getCrossSuiTangCity(int activityId, int groupId) {
        Map<Integer, Map<eSuiTangWarType, Map<Integer, Map<Integer, CrossSuiTangCity>>>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossSuiTangCitySql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossSuiTangCity city = parse(rs);
                Map<eSuiTangWarType, Map<Integer, Map<Integer, CrossSuiTangCity>>> warTypeMap = map.get(city.getRound());
                if (warTypeMap == null) {
                    warTypeMap = new ConcurrentHashMap<>();
                    warTypeMap.put(eSuiTangWarType.WAR_TYPE_YIJUN, new ConcurrentHashMap<>());
                    warTypeMap.put(eSuiTangWarType.WAR_TYPE_XIAOQI, new ConcurrentHashMap<>());
                    warTypeMap.put(eSuiTangWarType.WAR_TYPE_JINJUN, new ConcurrentHashMap<>());
                    map.put(city.getRound(), warTypeMap);
                }
                Map<Integer, CrossSuiTangCity> groupMap = warTypeMap.get(city.getWarType()).get(city.getActGroupId());
                if (groupMap == null) {
                    groupMap = new ConcurrentHashMap<>();
                    warTypeMap.get(city.getWarType()).put(city.getActGroupId(), groupMap);
                }
                groupMap.put(city.getCityId(), city);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossSuiTangCitySql = "update `t_c_suitang_city_data` set unionId=?,occupyTime=?,collectionTime=?,teamRecoveryBeginTime=?,lastRecoveryExtraTeamTime=?,status=?,npcGuardList=?,maxHp=?,changeStatusTime=?,needDefensePeople=?,updateDefensePeopleTime=? where activityId=? and groupId=? and actGroupId=? and round=? and warType=? and cityId=?";
    @Override
    protected boolean updateData(CrossSuiTangCity crossSuiTangCity) {
        crossSuiTangCity.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, crossSuiTangCity.getUnionId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangCity.getOccupyTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangCity.getCollectionTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangCity.getTeamRecoveryBeginTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangCity.getLastRecoveryExtraTeamTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCity.getStatus()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangCity.getNpcGuardList())));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCity.getMaxHp()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangCity.getChangeStatusTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCity.getNeedDefensePeople()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangCity.getUpdateDefensePeopleTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCity.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCity.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCity.getActGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCity.getRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangCity.getWarType())));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCity.getCityId()));
        if (getDb().executeNoneQuery(updateCrossSuiTangCitySql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossSuiTangCitySql = "insert into `t_c_suitang_city_data`(activityId,groupId,actGroupId,round,warType,cityId,unionId,occupyTime,collectionTime,teamRecoveryBeginTime,lastRecoveryExtraTeamTime,status,npcGuardList,maxHp,changeStatusTime,needDefensePeople,updateDefensePeopleTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossSuiTangCity crossSuiTangCity) {
        crossSuiTangCity.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCity.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCity.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCity.getActGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCity.getRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangCity.getWarType())));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCity.getCityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossSuiTangCity.getUnionId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangCity.getOccupyTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangCity.getCollectionTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangCity.getTeamRecoveryBeginTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangCity.getLastRecoveryExtraTeamTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCity.getStatus()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangCity.getNpcGuardList())));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCity.getMaxHp()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangCity.getChangeStatusTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCity.getNeedDefensePeople()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangCity.getUpdateDefensePeopleTime()));
        if (getDb().executeNoneQuery(insertCrossSuiTangCitySql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossSuiTangCity parseData(ResultSet rs) throws SQLException {
        CrossSuiTangCity crossSuiTangCity = new CrossSuiTangCity();
        crossSuiTangCity.setActivityId(rs.getInt("activityId"));
        crossSuiTangCity.setGroupId(rs.getInt("groupId"));
        crossSuiTangCity.setActGroupId(rs.getInt("actGroupId"));
        crossSuiTangCity.setRound(rs.getInt("round"));
        crossSuiTangCity.setWarType(JSON.parseObject(rs.getString("warType"), eSuiTangWarType.class));
        crossSuiTangCity.setCityId(rs.getInt("cityId"));
        crossSuiTangCity.setUnionId(rs.getString("unionId"));
        crossSuiTangCity.setOccupyTime(rs.getLong("occupyTime"));
        crossSuiTangCity.setCollectionTime(rs.getLong("collectionTime"));
        crossSuiTangCity.setTeamRecoveryBeginTime(rs.getLong("teamRecoveryBeginTime"));
        crossSuiTangCity.setLastRecoveryExtraTeamTime(rs.getLong("lastRecoveryExtraTeamTime"));
        crossSuiTangCity.setStatus(rs.getInt("status"));
        crossSuiTangCity.setNpcGuardList(JSON.parseObject(rs.getString("npcGuardList"), new TypeReference<Vector<Long>>(){}));
        crossSuiTangCity.setMaxHp(rs.getInt("maxHp"));
        crossSuiTangCity.setChangeStatusTime(rs.getLong("changeStatusTime"));
        crossSuiTangCity.setNeedDefensePeople(rs.getInt("needDefensePeople"));
        crossSuiTangCity.setUpdateDefensePeopleTime(rs.getLong("updateDefensePeopleTime"));

//        CrossSuiTangBattlefield battlefield = JSON.parseObject(rs.getString("battlefield"), CrossSuiTangBattlefield.class);
//        CrossSuiTangBattlefield battlefieldTemp = new CrossSuiTangBattlefield();
//        battlefieldTemp.setCreateTime(battlefield.getCreateTime());
//        battlefieldTemp.setLastBattleTime(battlefield.getLastBattleTime());
//        battlefieldTemp.setBattledNum(battlefield.getBattledNum());
//        for (Map.Entry<Long, CrossSuiTangBattlefieldTeam> entry : battlefield.getAtkTeamMap().entrySet()) {
//            battlefieldTemp.getAtkTeamMap().put(entry.getKey(), entry.getValue());
//        }
//        for (Map.Entry<Long, CrossSuiTangBattlefieldTeam> entry : battlefield.getDefTeamMap().entrySet()) {
//            battlefieldTemp.getDefTeamMap().put(entry.getKey(), entry.getValue());
//        }
//        for (Map.Entry<Long, CrossSuiTangBattlefieldTeam> entry : battlefield.getAttackingTeamMap().entrySet()) {
//            battlefieldTemp.getAttackingTeamMap().put(entry.getKey(), entry.getValue());
//        }
//        crossSuiTangCity.setBattlefield(battlefieldTemp);

        return crossSuiTangCity;
    }
}
